<?php
class classBasket extends phpajax{
	private $sSessionString;
	private $_Follow;
	
	function __construct(){
		$this->_Follow = new classFollow();
	}
	
	function input(){
		aread("ProductToBasket_ID");
		aread("ProductToBasket_Count");
		aread("ProductToBasket_Details");
        ///////
        aread("ProductType");
		//-----
		aread("basket_product_ID_remove");
		aread("basket_product_Detail_remove");
		//-----
		aread("basket_product_ArraiID_To_Show_Update_Form");
		aread("basket_product_ArraiID_To_Update");
		aread("basket_product_Count_To_Update");
                //-----
		aread("basket_Order_Make");
                //-----
		aread("basket_Clear_Session");
	}
	
	function loading() {} 
	
	function main() {    
            
		$iProductToBasket_ID = & $this->ProductToBasket_ID;
		$iProductToBasket_Count = & $this->ProductToBasket_Count;
		$iProductToBasket_Details = & $this->ProductToBasket_Details;
		//-----
		$iBasket_product_ID_remove = & $this->basket_product_ID_remove;
		$sBasket_product_Detail_remove = & $this->basket_product_Detail_remove;
		//-----
		$ibasket_product_ArraiID_To_Show_Update_Form = & $this->basket_product_ArraiID_To_Show_Update_Form;
		$ibasket_product_ArraiID_To_Update = & $this->basket_product_ArraiID_To_Update;
		$fbasket_product_Count_To_Update = & $this->basket_product_Count_To_Update;
		//-----
		$sBasket_Order_Make = & $this->basket_Order_Make;
        //-----
		$sBasket_Clear_Session = & $this->basket_Clear_Session;
                
		
		//-----------------------------------------------------------------
		//session
		$_basket_sess = new classSessions();
		$this->sSessionString = $_basket_sess->open('ShopBasket');
                
        if($sBasket_Clear_Session=='YES')
          $this->sSessionString = '';

        //if($iProductToBasket_Count=='' or $iProductToBasket_Count0<=0 or !is_numeric($iProductToBasket_ID))
        if(
			$iProductToBasket_ID!='' and $iProductToBasket_ID>0 and is_numeric($iProductToBasket_ID) and
			!($iProductToBasket_Count!='' and $iProductToBasket_Count>0 and is_numeric($iProductToBasket_Count)) and
			$iProductToBasket_Details!=''
		){
              aprint($this->ProductType.'product_insert_into_basket_result_container',_i18n_product_WRONG_ENTERED_COUNT_TO_BASKET_);
        }
		
		//-----------------------------------------------------------------
		//insert or update products into/in session
		if(
			$iProductToBasket_ID!='' and $iProductToBasket_ID>0 and is_numeric($iProductToBasket_ID) and
			$iProductToBasket_Count!='' and $iProductToBasket_Count>0 and is_numeric($iProductToBasket_Count) and
			$iProductToBasket_Details!=''
		){
			self::InsertOrUpdateProduct($iProductToBasket_ID, $iProductToBasket_Count, $iProductToBasket_Details);
			aprint($this->ProductType.'product_insert_into_basket_result_container',_i18n_product_PRODUCT_WAS_INSERTED_INTO_BASKET_);
		}
		
		//-----------------------------------------------------------------
		//update products with values from basket
		if(
			$ibasket_product_ArraiID_To_Update!='' and is_numeric($ibasket_product_ArraiID_To_Update) and $ibasket_product_ArraiID_To_Update>=0 and
			$fbasket_product_Count_To_Update!='' and is_numeric($fbasket_product_Count_To_Update) and $fbasket_product_Count_To_Update>0
		)
		{
			//update count in basket
			self::UpdateProduct(&$this->sSessionString, $ibasket_product_ArraiID_To_Update, $fbasket_product_Count_To_Update, 'substitution');
		}
		
		//-----------------------------------------------------------------
		//delete product
		if(
			$iBasket_product_ID_remove!='' and is_numeric($iBasket_product_ID_remove) and
			$sBasket_product_Detail_remove!=''
		)
			self::RemoveProduct(&$this->sSessionString, $iBasket_product_ID_remove, $sBasket_product_Detail_remove);
		
		//-----------------------------------------------------------------
		//show products list in basket
		aprint('basket_container',self::getList($this->sSessionString,$ibasket_product_ArraiID_To_Show_Update_Form));
			
		//-----------------------------------------------------------------
		//session serialize
		$_basket_sess->serializeIt($this->sSessionString);
	}
	
	
	private function InsertOrUpdateProduct($iProductToBasket_ID, $iProductToBasket_Count, $iProductToBasket_Details){
		//update or insert product
		$iInsertedProductArrayID = self::ProductIsInserted($this->sSessionString, $iProductToBasket_ID, $iProductToBasket_Details);
		if($iInsertedProductArrayID != -1)
			self::UpdateProduct(&$this->sSessionString, $iInsertedProductArrayID, $iProductToBasket_Count, 'addition');
		else
			self::InsertProduct(&$this->sSessionString, $iProductToBasket_ID, $iProductToBasket_Count, $iProductToBasket_Details);
	}
	
	private function RemoveProduct(&$sSessString, $iProductID, $sProductDetails){
		$iInsertedProductArrayID = self::ProductIsInserted($sSessString, $iProductID, $sProductDetails);
		if($iInsertedProductArrayID != -1){
			$basket_records = explode('/',$sSessString);
			unset($basket_records[$iInsertedProductArrayID]);
			$sSessString = implode('/',$basket_records);
		}
	}
	
	private function getList($sSessionString,$iUpdateFormIndex){
		$basket_records       = explode('/',$sSessionString);
		$basket_records_count = count($basket_records);
		
		//return $sSessionString;
		if($sSessionString=='') return '';
		
		$full_price = 0;
		
		$result = '
            <input type="hidden" name="basket_Clear_Session" id="basket_Clear_Session" value="">
            <input type="hidden" name="basket_product_ID_remove" id="basket_product_ID_remove" value="">
            <input type="hidden" name="basket_product_Detail_remove" id="basket_product_Detail_remove" value="">
            <input type="hidden" name="basket_product_ArraiID_To_Show_Update_Form" id="basket_product_ArraiID_To_Show_Update_Form" value="">
            <input type="hidden" name="basket_product_ArraiID_To_Update" id="basket_product_ArraiID_To_Update" value="">
            <table id="css_basket_content_table">
		';
		
		for($iIndex = 0; $iIndex<$basket_records_count; $iIndex++){
			$basket_records_array = explode(';',$basket_records[$iIndex]);
			$iProductID 	= & $basket_records_array[0];
			$iProductCount 	= & $basket_records_array[1];
			$iProductDetail	= & $basket_records_array[2];
			
			$sql_result = mysql_query('select Product_Name, Product_Price from products where ID='.$iProductID);
			$sql_object = mysql_fetch_object($sql_result);
			
			$result.='
				<tr>
					<td 
						class="css_basket_content_table_name_column"
						'.$this->_Follow->getJS($iProductID).'				
					>
						'.$sql_object->Product_Name.'
					</td>';
			
			if(
				$iUpdateFormIndex!='' and is_numeric($iUpdateFormIndex) and $iUpdateFormIndex>=0 and
				$iUpdateFormIndex == $iIndex
			)
				$result.= self::getUpdateForm($iUpdateFormIndex, $iProductCount);
			else
				$result.= '
					<td 
						class="css_basket_content_table_count_column"
						'.$this->_Follow->getJS(_i18n_basket_AFTER_CLICK_YOU_CAN_SHOW_FORM_FOR_UPDATE_OF_COUNT_).'
					>
						<a
							href = "'._URL_INACTIVE_ADDR_.'"
							onmousedown="
								document.getElementById(\'basket_product_ArraiID_To_Show_Update_Form\').value=\''.$iIndex.'\';
							"						
							onmouseup="
								document.getElementById(\'follow_container\').style.visibility = \'hidden\';
								classBasket();
							"
						>
							'.$iProductCount.'
						</a>
					</td>
				';
					
			$result.='
					<td 
						class="css_basket_content_table_delete_column"
						'.$this->_Follow->getJS(_i18n_basket_AFTER_CLICK_YOU_CAN_DELETE_PRODUCT_FROM_BASKET_).'
					>
						<a
							href="'._URL_INACTIVE_ADDR_.'"
							onmousedown="
								document.getElementById(\'basket_product_ID_remove\').value=\''.$iProductID.'\';
								document.getElementById(\'basket_product_Detail_remove\').value=\''.$iProductDetail.'\';
							"						
							onmouseup="
								document.getElementById(\'follow_container\').style.visibility = \'hidden\';
								classBasket();
							"
						>
							X
						</a>
					</td>
				</tr>
			';
			
			$products_list[$iIndex] = $iProductID;
			$counts_list[$iIndex] 	= $iProductCount;
		}
		
		$_Price = new classPriceExport();
		
		
		$result .='
				<tr>
					<td colspan="3" class="css_basket_full_price_container">
						'.$_Price->simpleResult($products_list, $counts_list).'
					</td>
				</tr>
				<tr>
					<td colspan="3" class="css_basket_full_price_container">
						<a
							href="'._URL_INACTIVE_ADDR_.'"
                            '.$this->_Follow->getJS(_i18n_basket_IT_SHOW_DETAIL_INFORMATION_ABOUT_BASKETS_CONTENT_).'
							onclick="'.$this->_Follow->getClosingJS().' classBasketsDetail();"
						>
							'._i18n_basket_DETAIL_OF_BASKET_.'
						</a>
					</td>
				</tr>
				<tr>
					<td colspan="3" class="css_basket_full_price_container">
						<a
							href="'._URL_INACTIVE_ADDR_.'"
                            '.$this->_Follow->getJS(_i18n_basket_IT_SHOW_FORM_FOR_ORDER_SENDING_).'
							onclick="'.$this->_Follow->getClosingJS().' classOrder();"
						>
							'._i18n_basket_SEND_ORDER_.'
						</a>
					</td>
				</tr>				
			</table>
		';
		
		return $result;
	}
	
	private function getUpdateForm($iArrayID, $fActualCount){		
		return '
			<td id="css_basket_update_form">
				<input type="text" name="basket_product_Count_To_Update" id="basket_product_Count_To_Update" value="'.$fActualCount.'">
				<a 
					href="'._URL_INACTIVE_ADDR_.'"
					onmousedown="
						document.getElementById(\'basket_product_ArraiID_To_Update\').value=\''.$iArrayID.'\';
					"
					onmouseup="classBasket();"
				>
					'._i18n_system_OK_.'
				</a>
				<a 
					href="'._URL_INACTIVE_ADDR_.'"
					onclick="classBasket();"
				>
					'._i18n_system_CANCEL_.'
				</a>
			</td>
		';
	}
	
	//----------------------------------------------------------
	//function tests, if product already has been inserted...
	//input parameters:
	//	$sSessString - 	in/out parameter - formated list of inserted products 
	//					format: ../ID;Count;Detail/... 
	//					where detail has format: .../ID;Count;A*B*C*D/...
	//	$iProductID -	ID number of product (from DB)
	//	$iProductDetail-Detail parameter which has format: .../ID;Count;A*B*C*D/...
	//return parameter:
	//	function returns ID of product, if it is inserted, or -1
	private function ProductIsInserted($sSessString, $iProductID, $iProductDetail){
		$basket_records 		= explode('/',$sSessString);
		$basket_records_count 	= count($basket_records);
		
		for($iIndex = 0; $iIndex<$basket_records_count; $iIndex++){
			$basket_records_array = explode(';',$basket_records[$iIndex]);
			if ($basket_records_array[0]==$iProductID and $basket_records_array[2]==$iProductDetail)
				return $iIndex;
		}
		
		return -1;
	}
	//----------------------------------------------------------
	
	
	//----------------------------------------------------------
	//function appends data into list
	//input parameters:
	//	$sSessString - 	in/out parameter - formated list of inserted products 
	//					format: ../ID;Count;Detail/... 
	//					where detail has format: .../ID;Count;A*B*C*D/...
	//	$iProductID -	ID number of product (from DB)
	//	$iProductCount -Count of product, inserted into basket
	//	$iProductDetail-Detail parameter which has format: .../ID;Count;A*B*C*D/...
	private function InsertProduct(&$sSessString, $iProductID, $iProductCount, $iProductDetail){
		if($sSessString=='')
			$sSessString =	$iProductID.';'.$iProductCount.';'.$iProductDetail; 
		else
			$sSessString.=	'/'.$iProductID.';'.$iProductCount.';'.$iProductDetail; 
	}
	//----------------------------------------------------------
	
	
	//----------------------------------------------------------
	//function updates data in list
	private function UpdateProduct(&$sSessString, $iArrayID, $iProductCount, $sOperation){
		$basket_records 		= explode('/',$sSessString);
		$basket_records_count 	= count($basket_records);

		$basket_records_array = explode(';',$basket_records[$iArrayID]);
		if($sOperation=='addition')
			$basket_records_array[1]+=$iProductCount;
		if($sOperation=='substitution')
			$basket_records_array[1]=$iProductCount;
			
		$basket_records[$iArrayID] = implode(';',$basket_records_array);
		$sSessString = implode('/',$basket_records);
		
		return 1;
	}
	//----------------------------------------------------------
}
?>